﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.03.22
 * 描 述：岗位管理
 */
var selectedRow;
var refreshGirdData;
var bootstrap = function ($, learun) {
    "use strict";
    var companyId = '';
    var departmentId = '';
    var page = {
        init: function () {
            page.inittree();
            page.initGrid();
            page.bind();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search({keyword: keyword});
                }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search({ keyword: keyword });
            });
            // 部门选择
            $('#department_select').lrselect({
                type: 'tree',
                placeholder: '请选择部门',
                // 展开最大高度
                maxHeight: 300,
                // 是否允许搜索
                allowSearch: true,
                select: function (item) {

                    if (!item || item.value == '-1') {
                        departmentId = '';
                    }
                    else {
                        departmentId = item.value;
                    }
                    page.search();
                }
            });

            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 新增
            $('#lr_add').on('click', function () {
                if (!companyId) {
                    learun.alert.warning('请选择机构！');
                    return false;
                }
                selectedRow = null;
                learun.layerForm({
                    id: 'form',
                    title: '添加岗位',
                    url: config.webportal_server + 'pages/system/post/form.html?access_token=' + config.getToken().access_token + '&companyId=' + companyId,
                    width: 500,
                    height: 379,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '编辑岗位',
                        url: config.webportal_server + 'pages/system/post/form.html?access_token=' + config.getToken().access_token + '&companyId=' + companyId,
                        width: 500,
                        height: 379,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('该动作执行后此项将彻底被删除，是否确认删除该项?', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'posts/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
            // 添加岗位成员
            $('#lr_memberadd').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                selectedRow = $('#gridtable').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '添加岗位成员',
                        url: config.webportal_server + 'pages/system/userRelation/selectForm.html?objectId=' + keyValue + '&companyId=' + companyId + '&departmentId=' + selectedRow.departmentId + '&objectType=Post&access_token=' + config.getToken().access_token,
                        width: 800,
                        height: 520,
                        callBack: function (id) {
                            return top[id].acceptClick();
                        }
                    });
                }
            });
            // 查看成员
            $('#lr_memberlook').on('click', function () {
                var keyValue = $('#gridtable').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '查看岗位成员',
                        url: config.webportal_server + 'pages/system/userRelation/lookForm.html?objectId=' + keyValue + '&access_token=' + config.getToken().access_token,
                        width: 800,
                        height: 520,
                        btn: null
                    });
                }
            });
            // 数据导出
            $('#lr_export').on('click', function () {
                location.href = config.base_server() + 'postexcel/export?access_token=' + config.getToken().access_token;
            });
        },
        inittree: function () {
            $('#companyTree').lrtree({
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                param: { parentId: '0' },
                nodeClick: page.treeNodeClick
            });
            $('#companyTree').lrtreeSet('checkFirst');
        },
        treeNodeClick: function (item) {
            companyId = item.id;
            $('#titleinfo').text(item.text);

            $('#department_select').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'departments/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: { companyId: companyId, parentId: '0' },
            });
            departmentId = '';
            page.search();
        },
        initGrid: function () {
            $('#gridtable').jfGrid({
                url: config.base_server() + 'posts?access_token=' + config.getToken().access_token,
                headData: [
                    { label: "岗位名称", name: "name", width: 300, align: "left" },
                    { label: "岗位编号", name: "code", width: 100, align: "left" },
                    { label: "所属部门", name: "departmentName", width: 120, align: "left" },
                    {
                        label: "默认岗位", name: "isDefault", width: 80, align: "center",
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "是" : "";
                        }
                    },
                    {
                        label: "有效", name: "status", width: 50, align: "center",
                        formatter: function (cellvalue) {
                            return cellvalue == 1 ? "<i class=\"fa fa-toggle-on\"></i>" : "<i class=\"fa fa-toggle-off\"></i>";
                        }
                    },
                    {
                        label: "更新时间", name: "updateTime", width: 140, align: "center",
                        formatter: function (value) {
                            return learun.formatDate(value, 'yyyy-MM-dd hh:mm:ss');
                        }
                    },
                    { label: "备注", name: "description", width: 200, align: "left"}

                ],
                dblclick: function (row) {
                    var keyValue = $('#gridtable').jfGridValue('id');
                    selectedRow = row;
                    if (learun.checkrow(keyValue)) {
                        learun.layerForm({
                            id: 'form',
                            title: '编辑岗位',
                            url: config.webportal_server + 'pages/system/post/form.html?access_token=' + config.getToken().access_token + '&companyId=' + companyId,
                            width: 500,
                            height: 379,
                            callBack: function (id) {
                                return top[id].acceptClick(refreshGirdData);
                            }
                        });
                    }
                },
                isPage: true,
                reloadSelected: true,
                isTree: true,
                mainId: 'id',
                parentId: 'parentId',
            });
        },
        search: function (param) {
            param = param || {};
            param.companyId = companyId;
            param.departmentId = departmentId;

            $('#gridtable').jfGridSet('reload', param);
        }
    };

    refreshGirdData = function () {
        page.search();
    };

    page.init();
}


